#coding=utf-8
import stutils

def get_ipo_from_soup(soup):
    t = unicode('上市日期', 'u8')
    
    rq = soup.find(text=lambda x: x.find(t) !=  -1)
    if not rq:return None
    
    date = rq.parent.parent.nextSibling.nextSibling.string
    date = date.encode('u8')
    return date

def get_ipo(code):
    url = "http://stock.business.sohu.com/q/gsjs.php?&code=%s"%code
    
    s = stutils.get_soup(url)
    date = get_ipo_from_soup(s)
    
    return date
    
if __name__ == "__main__":
    from stsettings import DIRNAME
    from stvalidator import isValidExdate,ValidationError
    
    cnsfile = DIRNAME+ '/data/stcode.csv'
    ipofile = DIRNAME+ '/data/ipo.csv'
    
    codes = stutils.stcode_from_csv(cnsfile)
    
    data = []
    for c in codes:
        ipo = get_ipo(c)   
        
        if not ipo:
            print "No ipo date for %s"%c 
            continue
            
        ### validate
        try:
            isValidExdate(ipo)
        except ValidationError, e:
            print c
            print e,'\n'
            continue
                
        data.append([c, ipo])
        
    head= ['代码', '上市日期']
    
    stutils.data2csv(data, head, ipofile)
